package com.base.utils;

import java.lang.reflect.Field;

import com.base.exception.EmsRuntimeException;

public class ReflectUtils {

	/**
	 * 获得类的除去包前缀的类名
	 * @param objClass
	 * @return
	 */
	public static String getClassName(Class<?> objClass){
		String className = objClass.getName();
		className = className.substring(className.lastIndexOf(".")+1);
		return className;
	}
	
	/**
	 * 设置某一实体类某个属性的值
	 * @param objClass 类名
	 * @param object 实体类
	 * @param fieldName 属性名
	 * @param value 要设置的属性值
	 * @throws NoSuchFieldException 
	 * @throws SecurityException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static void setFieldValue(Object object,String fieldName,Object value){
		Class<?> objClass=object.getClass();
		Field field;
		try {
			field = objClass.getDeclaredField(fieldName);
			field.setAccessible(true);//禁用安全检查
			field.set(object, value);
		} catch (NoSuchFieldException e) {
			throw new EmsRuntimeException(e);
		} catch (SecurityException e) {
			throw new EmsRuntimeException(e);
		} catch (IllegalArgumentException e) {
			throw new EmsRuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new EmsRuntimeException(e);
		}
	}
}
